*This file prepares the datasets for industry-level analysis in Finland and Norway.

* Set global path for easier access to data directories.
cd "$path"

* Load the main industry data set.
use "$path/industry_data.dta", clear

* Load  data on education by industry, and generate a new variable
preserve
use "$path/ind_edu.dta", clear
tempfile education
gen low_edu=(high_edu!=1)
save `education' 
restore

* Merge the education data with the main data set.
joinby digt2 using `education' , unmatched (master) 

*Generate education-level interactions
preserve
keep if year==1943
replace high_edu=0 if high_edu==.&repa_sharel==0
replace low_edu=. if high_edu==.
gen h_std=repa_sharel*high_edu
egen h_std2=std(h_std)
gen l_std=repa_sharel*low_edu
egen l_std2=std(l_std)
keep isic h_std2 l_std2 low_edu high_edu
tempfile education2
save `education2' 
restore


* Merge the variables
joinby isic using `education2' 
keep if year==1943
keep isic treat_std_l repa_tot repa_sharel  decile_repashare_2 h_std  h_std2 l_std l_std2 low_edu high_edu d2_s d1_s
rename isic finnish_isic
levelsof finnish_isic if repa_sharel > 0
tempfile treat
save `treat'


*Input the consistent and balanced Norwegian industry-level panel data
use "$path/nor_panel_ind", clear

*Already changed into 1935 Finnish marks and deflated using Norwegian sales price index
rename GVoP_kr gross_value_real  
rename lwages1943 lnwage1943
gen ln_value_prod=ln(gross_value_real)
gen ln_productivity=ln(gross_value_real/Labor_all)
gen lnlabor_all=ln(Labor_all)
gen wage43=exp(lnwage1943)
gen ln_relative_wage1943=ln(exp(lnwage1943)/Labor_all)

*Find the Norwegian couterparts of the exposed Finnish industries 
gen finnish_isic=2511 if isic_1969=="2510"
replace finnish_isic=2711 if isic_1969=="2710"
replace finnish_isic=2713 if isic_1969=="2721"
replace finnish_isic=2714 if isic_1969=="2722"
replace finnish_isic=2714 if isic_1969=="2722"
replace finnish_isic=3299 if isic_1969=="3200"
replace finnish_isic=3311 if isic_1969=="3310"
replace finnish_isic=3393 if isic_1969=="3399"
replace finnish_isic=3413 if isic_1969=="3413"
replace finnish_isic=3508 if isic_1969=="3513"
replace finnish_isic=3519 if isic_1969=="3599"
replace finnish_isic=3630 if isic_1969=="3610"
replace finnish_isic=3710 if isic_1969=="3712"
replace finnish_isic=3740 if isic_1969=="3711"
replace finnish_isic=3751 if isic_1969=="3713"
replace finnish_isic=3769 if isic_1969=="3799"
replace finnish_isic=3811 if isic_1969=="3812"
replace finnish_isic=3812 if isic_1969=="3811"
replace finnish_isic=3820 if isic_1969=="3821"
replace finnish_isic=3839 if isic_1969=="3839"
replace finnish_isic=3930 if isic_1969=="3999"
replace finnish_isic=1590 if isic_1969=="1590" 
replace finnish_isic=2511 if isic_1969=="2510"
replace finnish_isic=2711 if isic_1969=="2710"
replace finnish_isic=2713 if isic_1969=="2721"
replace finnish_isic=2714 if isic_1969=="2722"
replace finnish_isic=2715 if isic_1969=="2730"
replace finnish_isic=2716 if isic_1969=="2730" 
replace finnish_isic=3119 if isic_1969=="3119"
replace finnish_isic=3299 if isic_1969=="3290"
replace finnish_isic=3311 if isic_1969=="3310"
replace finnish_isic=3340 if isic_1969=="3340"
replace finnish_isic=3393 if isic_1969=="3399"
replace finnish_isic=3411 if isic_1969=="3411"
replace finnish_isic=3413 if isic_1969=="3413"
replace finnish_isic=3508 if isic_1969=="3513"  
replace finnish_isic=3510 if isic_1969=="3599"
replace finnish_isic=3519 if isic_1969=="3599"
replace finnish_isic=3620 if isic_1969=="3610"
replace finnish_isic=3630 if isic_1969=="3699"
replace finnish_isic=3710 if isic_1969=="3712"
replace finnish_isic=3720 if isic_1969=="3791"
replace finnish_isic=3740 if isic_1969=="3711"  
replace finnish_isic=3751 if isic_1969=="3713"
replace finnish_isic=3752 if isic_1969=="3719"
replace finnish_isic=3761 if isic_1969=="2312"
replace finnish_isic=3769 if isic_1969=="3799"
replace finnish_isic=3811 if isic_1969=="3812"
replace finnish_isic=3812 if isic_1969=="3811"
replace finnish_isic=3820 if isic_1969=="3821"  
replace finnish_isic=3831 if isic_1969=="3831"
replace finnish_isic=3832 if isic_1969=="3831"
replace finnish_isic=3839 if isic_1969=="3839"
replace finnish_isic=3911 if isic_1969=="3910"


*Join the treatment data to the assigned Norwegian industries
tostring finnish_isic, replace

joinby finnish_isic using `treat', unmatched(master)
replace repa_sharel=0 if repa_sharel==. //zeros or the smallest value in standardized

*Create Controls

forvalues  x=34/43 {
bys isic: gen ln_value_prod19`x'1=ln_value_prod if year==19`x'
egen ln_value_prod19`x' = mean(ln_value_prod19`x'1), by(isic)
drop ln_value_prod19`x'1
}	

forvalues  x=34/43 {
bys isic: gen lnlabor_all19`x'1=lnlabor_all if year==19`x'
egen lnlabor_all19`x' = mean(lnlabor_all19`x'1), by(isic)
drop lnlabor_all19`x'1
}	
   
global sharecontrols c.ln_value_prod1943#year c.lnlabor_all1943#year 


preserve
use "$path/industry_data.dta", clear
joinby isic using `education2' 
keep  ln_productivity ln_power ln_value_prod lnlabor_all ln_value_prod* lnlabor_all*     isic year scaledpost treat_std_l post repa_sharel  decile_repashare_2  h_std2 l_std2 low_edu high_edu  d2_s  d1_s 
tempfile finland
save `finland'
restore

gen post=(year>1943)
gen finland=0
rename isic_1969 isic

*Append with the Finnish data

append using `finland'
replace finland=1 if finland==.
egen  isic_country=group(isic finland)

*Standardize for the whole sample
preserve
keep if year==1943
keep isic_country repa_sharel
egen treat_std_l3=std(repa_sharel)
keep isic_country treat_std_l3
tempfile treats
save `treats'
restore

drop if year==1970
joinby isic_country using `treats'

global treat treat_std_l3
gen treat_post=$treat*post
gen finland_post=finland*post
gen scaledpost_finland=finland*$treat*post

gen isic_2=isic
gen twodigit=substr(isic_2,1,2)
gen timberpaper=(twodigit=="25"|twodigit=="27")
global sharecontrols2 c.ln_value_prod1943#year c.lnlabor_all1943#year 
egen  year_country=group(year finland)

****
*Heterogeneity
*****

replace h_std2=0 if h_std2==.&repa_sharel==0 // zero value for the non-exposed Norwegian
replace l_std2=0 if l_std2==.&repa_sharel==0

gen high_edu2=(twodigit=="36"|twodigit=="37"|twodigit=="38"|twodigit=="27"|twodigit=="32"|twodigit=="39")

replace high_edu2=. if twodigit=="15"
replace high_edu2=. if twodigit=="31"

gen low_edu2=(high_edu2==0)
gen treat_post_he=h_std2*post*high_edu2
gen treat_post_le=l_std2*post*low_edu2
gen treat_post_he_fin=h_std2*post*finland
gen treat_post_le_fin=l_std2*post*finland

****
*Groups
*****

replace decile_repashare_2=0 if decile_repashare_2==.
gen  high_finland_post=finland*(decile_repashare_2==2)*post
gen  low_finland_post=finland*(decile_repashare_2==1)*post
gen  high_post=(decile_repashare_2==2)*post
gen  low_post=(decile_repashare_2==1)*post

global treat_dummies  high_finland_post  low_finland_post high_post  low_post




* Label the variables
label variable year "Year"
label variable isic "Industry (ISIC Code)"
label variable gross_value_real "Real Gross Value of Output"
label variable Labor_all "Total Labor"
label variable skilled1943 "Skilled Labor in 1943"
label variable lnwage1943 "Log of Wages in 1943"
label variable number "Number of Units/Entities"
label variable ln_value_prod "Log of Value Produced"
label variable ln_productivity "Log of Productivity"
label variable lnlabor_all "Log of Total Labor"
label variable wage43 "Wages in 1943"
label variable ln_relative_wage1943 "Log of Relative Wage in 1943"
label variable finnish_isic "Finnish ISIC Code"
label variable _merge "Merge Indicator"
label variable repa_tot "Total Reparations"
label variable repa_sharel "Reparations Share"
label variable treat_std_l "Standardized Treatment"
label variable decile_repashare_2 "Decile of Reparations Share"
label variable d2_s "Second Decile Indicator"
label variable d1_s "First Decile Indicator"
label variable high_edu "High Education Indicator"
label variable low_edu "Low Education Indicator"
label variable h_std2 "Standardized High Education"
label variable l_std2 "Standardized Low Education"
label variable ln_value_prod1934 "Log of Value Produced in 1934"
label variable ln_value_prod1935 "Log of Value Produced in 1935"
label variable ln_value_prod1936 "Log of Value Produced in 1936"
label variable ln_value_prod1937 "Log of Value Produced in 1937"
label variable ln_value_prod1938 "Log of Value Produced in 1938"
label variable ln_value_prod1939 "Log of Value Produced in 1939"
label variable ln_value_prod1940 "Log of Value Produced in 1940"
label variable ln_value_prod1941 "Log of Value Produced in 1941"
label variable ln_value_prod1942 "Log of Value Produced in 1942"
label variable ln_value_prod1943 "Log of Value Produced in 1943"
label variable lnlabor_all1934 "Log of Total Labor in 1934"
label variable lnlabor_all1935 "Log of Total Labor in 1935"
label variable lnlabor_all1936 "Log of Total Labor in 1936"
label variable lnlabor_all1937 "Log of Total Labor in 1937"
label variable lnlabor_all1938 "Log of Total Labor in 1938"
label variable lnlabor_all1939 "Log of Total Labor in 1939"
label variable lnlabor_all1940 "Log of Total Labor in 1940"
label variable lnlabor_all1941 "Log of Total Labor in 1941"
label variable lnlabor_all1942 "Log of Total Labor in 1942"
label variable lnlabor_all1943 "Log of Total Labor in 1943"
label variable post "Post-Treatment Indicator"
label variable finland "Finland Indicator"
label variable ln_power "Log of Power Consumption"
label variable isic_country "ISIC and Country Code"
label variable treat_std_l3 "Standardized Reparations share"
label variable treat_post "Reparations share x Post"
label variable finland_post "Finland x Post"
label variable scaledpost_finland "Reparations share x Finland"
label variable isic_2 "Two-Digit ISIC Code"
label variable twodigit "Two-Digit Industry Code"
label variable timberpaper "Timber and Paper Industry Indicator"
label variable year_country "Year and Country Indicator"
label variable high_edu2 "High Education"
label variable low_edu2 "Low Education"
label variable treat_post_he "Treatment x Post x HS"
label variable treat_post_le "Treatment x Post x LS"
label var treat_post_he "Reparations share x Post x HS"
label var treat_post_le "Reparations share x Post x LS"
label var treat_post_he_fin "Reparations share x Post x Finland x HS"
label var treat_post_le_fin "Reparations share x Post x Finland x LS"
label variable high_post "High Post Indicator"
label variable low_post "Low Post Indicator"


*Save data
save  "$path/ind_ddd.dta", replace

